% Generalized Gaussian Distribution
% PDF (version 2 in: https://en.wikipedia.org/wiki/Generalized_normal_distribution)
% Location (real)
% Scale (>0)
% Shape (real, but restrict it between -1 and 1 for reasonable outcomes.
% also high values get log() to complex numbers
function[tx]=ggd_pdf(x,theta)
location=theta(1);
scale=theta(2);
shape=theta(3);
            normdb = makedist('norm',0,1);
            if shape == 0
                y = (x-location)./scale;
                tx = pdf(normdb,y);
            elseif shape>0
                if x<= location+(scale/shape)
                    y = -(1./shape).*log(1- ((shape.*(x-location))./scale));
                    tx = pdf(normdb,y)./(scale-shape.*(x-location));

                else
                    tx = 0;
                end

            else %shape<0
                if x>= location+(scale/shape)
                    y = -(1./shape).*log(1- ((shape.*(x-location))./scale));
                    tx = pdf(normdb,y)./(scale-shape.*(x-location));
                else
                    tx = 0;
                end

            end
            tx=ones(size(tx)).*tx;%necessary otherwise integrate() based on this CDF requires ArrayValued=TRUE and that is crazy slow

end
        